package com.fmsz.xmly.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fmsz.xmly.R;
import com.fmsz.xmly.activitys.AnchorMoreActivity;
import com.fmsz.xmly.entitys.findanchor.AnchorEntity;

import java.util.ArrayList;

/**
 * Created by lenovo on 2016/12/4.
 */

public class AnchorAdapter extends RecyclerView.Adapter<AnchorAdapter.ViewHolder>{
    private ArrayList datas;
    private Context context;
    private AnchorContentAdapter adapter;

    public AnchorAdapter(Context context, ArrayList datas){
        this.datas = datas;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_anchor,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final AnchorEntity entity = (AnchorEntity) datas.get(position);
        holder.tvTitle.setText(entity.getTitle());
        ArrayList list = (ArrayList) entity.getList();
        adapter = new AnchorContentAdapter(context,list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        holder.recyclerView.setLayoutManager(linearLayoutManager);
        holder.recyclerView.setAdapter(adapter);

        holder.tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, AnchorMoreActivity.class);
                intent.putExtra("title",entity.getTitle());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas==null?0:datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tvTitle;
        private TextView tvMore;
        private RecyclerView recyclerView;
        public ViewHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvMore = (TextView) itemView.findViewById(R.id.tv_more);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.anchor_recycler);
        }
    }
}
